package com.jeesite.modules.rt.web;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.rt.entity.RtBanner;
import com.jeesite.modules.rt.service.RtBannerService;

/**
 * 轮播图Controller
 * @author 李寒冰
 * @version 2024-11-20
 */
@Controller
@RequestMapping(value = "${adminPath}/rt/rtBanner")
public class RtBannerController extends BaseController {

	@Autowired
	private RtBannerService rtBannerService;

	@Value("${pic.baseUrl}")
	private String baseUrl;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public RtBanner get(String id, boolean isNewRecord) {
		return rtBannerService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("rt:rtBanner:view")
	@RequestMapping(value = {"list", ""})
	public String list(RtBanner rtBanner, Model model) {
		model.addAttribute("rtBanner", rtBanner);
		return "modules/rt/rtBannerList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("rt:rtBanner:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<RtBanner> listData(RtBanner rtBanner, HttpServletRequest request, HttpServletResponse response) {
		rtBanner.setPage(new Page<>(request, response));
		Page<RtBanner> page = rtBannerService.findPage(rtBanner);
		List<RtBanner> list = page.getList();
		for (RtBanner r : list) {
			r.setPic(baseUrl+r.getPic());
		}
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("rt:rtBanner:view")
	@RequestMapping(value = "form")
	public String form(RtBanner rtBanner, Model model) {
		model.addAttribute("rtBanner", rtBanner);
		return "modules/rt/rtBannerForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("rt:rtBanner:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated RtBanner rtBanner) {
		rtBanner.setCreateTime(new Date());
		rtBannerService.save(rtBanner);
		return renderResult(Global.TRUE, text("保存轮播图成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("rt:rtBanner:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(RtBanner rtBanner) {
		rtBannerService.delete(rtBanner);
		return renderResult(Global.TRUE, text("删除轮播图成功！"));
	}
	
}